package com.spyu.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

/**
 * JSON工具类
 *
 * @author spyu
 * @date 2023-03-08
 */
public class JsonUtils {

    /**
     * 将Map转为json串
     *
     * @param paramMap
     * @return
     */
    public static String mapToJsonStr(Map<String, Object> paramMap) {
        ObjectMapper objectMapper = new ObjectMapper();
        String resultStr = null;

        try {
            resultStr = objectMapper.writeValueAsString(paramMap);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return resultStr;
    }

    /**
     * 将json串转为Map
     *
     * @param jsonStr
     * @return
     */
    public static Map<String, String> strToJson(String jsonStr) {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> map = null;

        try {
            map = objectMapper.readValue(jsonStr, Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return map;
    }
}
